//
//  UserManager.m
//  HotWord
//
//  Created by Rainbow on 7/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UserManager.h"
#import "ITTObjectSingleton.h"
#import "MessageManager.h"

#define LOGIN_USER  @"LOGIN_USER"
#define ALL_SCHOOLS @"ALL_SCHOOLS"

@interface UserManager()

- (void)getLoginUser;
@end

@implementation UserManager

ITTOBJECT_SINGLETON_BOILERPLATE(UserManager, sharedManager)
@synthesize currentUser;
@synthesize currentLocation2D;
@synthesize nearByUsers;
@synthesize cacheUsers;
@synthesize colleageUsers;
@synthesize questionTags;

@synthesize schools;

- (id)init{
    self = [super init];
	if (self) {
		[self restore];
	}
	return self;
}

- (void)restore{
    self.nearByUsers = [NSMutableArray array];
    self.cacheUsers = [NSMutableArray array];
    self.colleageUsers = [NSMutableArray array];
    self.questionTags = [NSMutableArray array];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *shoolData = [defaults objectForKey:ALL_SCHOOLS];
    if (shoolData) {
        self.schools = [NSKeyedUnarchiver unarchiveObjectWithData:shoolData];
    }
    [self getLoginUser];
}

- (void)saveLoginUser:(User *)user
{
    if (!user) {
        return;
    }
    self.currentUser = user;
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
//    [defaults setObject:userData forKey:LOGIN_USER];
//    [defaults synchronize];
    [self saveLoginUser];
}

- (void)saveLoginUser
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:self.currentUser];
    [defaults setObject:userData forKey:LOGIN_USER];
    [defaults synchronize];
}

- (void)getLoginUser
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *userData = [defaults objectForKey:LOGIN_USER];
    if (userData) {
        self.currentUser = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
    }
}

- (void)removeLoginUser
{
    [[MessageManager sharedManager] clearMessages];
    [self.nearByUsers removeAllObjects];
    [self.cacheUsers removeAllObjects];
    [self.colleageUsers removeAllObjects];
    self.currentUser = nil;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:LOGIN_USER];
    [defaults synchronize];
}

- (User *)getCacheUser:(NSString *)userId
{
    for (User *user in self.cacheUsers) {
        if ([userId isEqualToString:user.userId]) {
            return user;
        }
    }
    return nil;
}

- (void)setAllSchools:(NSMutableArray *)allSchools
{
    self.schools = allSchools;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *schoolData = [NSKeyedArchiver archivedDataWithRootObject:self.schools];
    [defaults setObject:schoolData forKey:ALL_SCHOOLS];
    [defaults synchronize];
}

- (void)dealloc{
    [schools release];
    [cacheUsers release];
    [nearByUsers release];
    [currentUser release];
    [questionTags release];
	[super dealloc];
}

@end
